 //
//  PrivateMsgSender.m
//  SportSocial
//
//  Created by yull on 15/1/3.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "PrivateMsgSender.h"
#import "PrivateChatMsg.h"
#import "PrivateIMNet.h"
#import "CurrentChat.h"
#import "PrivateMsgDao.h"

@implementation PrivateMsgSender

- (instancetype)initWithUser:(UserModel *)user
{
    if (self = [super init]) {
        self.chatUser = user;
        self.chatObjectId = user.userId;
        self.name = user.name;
        [CurrentChat shareInstance].chatUser = user;
    }
    return self;
}

- (void)addCommonFieldWithChatMsg:(PrivateChatMsg *)privateMsg
{
    privateMsg.sendId = [CurrentUser shareInstance].userId;
    privateMsg.senderName = self.chatUser.name;
    privateMsg.createTime = [[NSDate date] timeIntervalSince1970] * 1000;
    privateMsg.roleType = ChatMsgRoleTypeSend;
    privateMsg.receiveId = self.chatObjectId;
    privateMsg.isRead = YES;
    privateMsg.isAck = YES;
    privateMsg.sendStatus = ChatMsgSendStatusSending;
    privateMsg.retainField = @"";
    privateMsg.picUploadPercent = 0.0f;
    privateMsg.icon = self.chatUser.icon;
    privateMsg.sex = [CurrentUser shareInstance].sex;
}

- (void)createMsgWithText:(NSString *)text
{
    PrivateChatMsg *privateMsg = [[PrivateChatMsg alloc] init];
    privateMsg.content = text;
    privateMsg.msgType = ChatMsgTypeText;
    privateMsg.msgSubType = ChatMsgSubTypeText;
    privateMsg.decodeContent = text;
    privateMsg.msgLength = 0;
    
    [self addCommonFieldWithChatMsg:privateMsg];
    
    [[PrivateIMNet shareInstance] sendPrivateTextMsg:privateMsg isResend:NO];
}

- (void)createMsgWithAudioData:(NSData *)audioData
                   audioLength:(NSInteger)audioLength
                          path:(NSString *)filePath
{
    PrivateChatMsg *privateMsg = [[PrivateChatMsg alloc] init];
    privateMsg.content = @"语音";
    privateMsg.msgType = ChatMsgTypeAudio;
    privateMsg.msgSubType = ChatMsgSubTypeAudio;
    privateMsg.decodeContent = privateMsg.content;
    privateMsg.msgLength = audioLength;
    privateMsg.audioRestTime = audioLength;

    [self addCommonFieldWithChatMsg:privateMsg];
    
    //privateMsg.localPath = [FunctionMethod createAudioPath];
    privateMsg.localPath = filePath;
    //BOOL ret = [audioData writeToFile:privateMsg.localPath atomically:YES];
    //if (ret) {
        NSLog(@"保存成功!");
        privateMsg.audioStatus = ChatMsgAudioStatusNone;
        
        [[PrivateIMNet shareInstance] sendPrivateAudioMsg:privateMsg audioData:audioData isResend:NO];
    //}
}

- (void)createMsgWithImage:(UIImage *)image
{
    PrivateChatMsg *privateMsg = [[PrivateChatMsg alloc] init];
    privateMsg.content = @"图片";
    privateMsg.msgType = ChatMsgTypePic;
    privateMsg.msgSubType = ChatMsgSubTypePic;
    privateMsg.decodeContent = privateMsg.content;
    privateMsg.msgLength = 0;
    
    [self addCommonFieldWithChatMsg:privateMsg];
    
    NSData *imageData = UIImageJPEGRepresentation(image, 0.1f);
    
    privateMsg.localPath = [FunctionMethod createPicPath];
    BOOL ret = [imageData writeToFile:privateMsg.localPath atomically:YES];
    if (ret) {
        NSLog(@"保存成功!");
        [[PrivateIMNet shareInstance] sendPrivatePicMsg:privateMsg imageData:imageData isResend:NO];
    }
}

- (NSArray *)selectChatMsg
{
    return [[PrivateMsgDao shareInstance] selectAllPrivateMsgArrayWithUser:self.chatObjectId];
}

- (NSArray *)selectHistoryMsgWithDbId:(UInt64)dbId
{
    return [[PrivateMsgDao shareInstance] selectHistoryPrivateMsgArrayWithUser:self.chatObjectId dbId:dbId];
}

- (BOOL)updateReadWithMsgId:(UInt64)msgId
{
    return [[PrivateMsgDao shareInstance] updateReadWithDBId:msgId];
}

- (NSArray *)selectAllPicMsg
{
    return [[PrivateMsgDao shareInstance] selectAllPicMsgWithUserId:self.chatUser.userId];
}

//重新发送
- (void)resendMsg:(ChatBaseMsg *)msg
{
    msg.sendStatus = ChatMsgSendStatusSending;
    
    PrivateChatMsg *chatMsg = (PrivateChatMsg *)msg;
    if (msg.msgType == ChatMsgTypeText) {
        [[PrivateIMNet shareInstance] sendPrivateTextMsg:chatMsg isResend:YES];
    }else if (msg.msgType == ChatMsgTypePic) {
        NSData *imageData = [NSData dataWithContentsOfFile:msg.localPath];
        
        [[PrivateIMNet shareInstance] sendPrivatePicMsg:chatMsg imageData:imageData isResend:YES];
    }else {
        NSData *audioData = [NSData dataWithContentsOfFile:msg.localPath];
        
        [[PrivateIMNet shareInstance] sendPrivateAudioMsg:chatMsg audioData:audioData isResend:YES];
    }
}
@end
